import loginView from '$v/login';
import { loginRequest } from '$a/admin';

export default (req, res) => {
  res.render(loginView())
  // 点击登录逻辑
  document.loginForm.loginBtn.onclick = async function (e) {
    // 阻止浏览器默认事件
    e.preventDefault()
    // 获取元素
    const adminNameValue = document.loginForm.adminName.value
    const passwordValue = document.loginForm.passWord.value
    try {
      const loginResult = await loginRequest({
        adminName: adminNameValue,
        password: passwordValue
      })
      // 登录成功可以拿到token,把token保存在localStorage中
      localStorage.setItem("user_token", loginResult.token)
      router.go("/adv/adminList")
    } catch (e) {
      alert("登录异常" + e.message)
    }
  }
}